package video.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import common.Resquest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import resgistor.Video;
import resgistor.VideoOrder;
import video.dao.VideoDao;

import javax.annotation.Resource;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;

import static java.lang.Thread.sleep;
@EnableFeignClients
@EnableDiscoveryClient
@Service
public class VideoServices {
    @Resource
    Resquest resquest;
    @Resource
    VideoDao videoDao;

    @ResponseBody
    public Resquest getVideo(Integer id){
        if(id!=null){
            System.out.println(000);
            Video video = videoDao.searchVideoById(id);
            System.out.println("id");
            System.out.println(video);
            return resquest.sueccess(200, "成功00", video);
        }
       return resquest.error(401, "失败");
    }
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate=new RestTemplate();
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return restTemplate;
    }
    @Autowired
    RestTemplate restTemplate;
    @ResponseBody
    public Resquest getVideoOrder( Integer id){
        if(id!=null){
            Video video = videoDao.searchVideoOrderById(id);
            HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();

            if(video!=null){
                factory.setConnectTimeout(5000);
                factory.setReadTimeout(10000);
                factory.setBufferRequestBody( true);

                restTemplate.setRequestFactory(factory);
                System.out.println(video.toString());
               restTemplate.getForObject("http://localhost:8080/api/order/c/addVideoOrder?id="+video.getId()+"&title="+video.getTitle(), resgistor.Video.class,video);
//                System.out.println(video1.getCreateTime().getTimezoneOffset());
//                System.out.println(video1.toString());
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return resquest.sueccess(200, "成功", video);
            }

        }
      return resquest.error(401, "失败");

    }
    @Autowired
    DiscoveryClient discoveryClient;
    @Bean
    VideoOrder video(){
        return new VideoOrder();
    }
    @Autowired
    VideoOrder videoOrder;
    @ResponseBody
    public Integer getVideoOrder2(Integer id) throws MalformedURLException, InterruptedException {
        if(Objects.nonNull(id)){
           Video video= videoDao.searchVideoById(id);
           if(video!=null){
              List<ServiceInstance> orderServices= discoveryClient.getInstances("xdclass-order-service");
//                restTemplate.getForObject(orderServices.getUri()+"/api/order/c/addVideoOrder?id="+video.getId()+"&title="+video.getTitle(), resgistor.Video.class,video);
//               restTemplate.getForObject("http://"+orderServices.get(0).getHost()+":"+orderServices.get(0).getPort()+"/api/order/c/addVideoOrder?id="+video.getId()+"&title="+video.getTitle(), resgistor.Video.class,video);
               String url="http://"+"xdclass-order-service"+"/api/order/c/searchOrder";
               String url2="?id="+video.getId()+"&title="+video.getTitle();
               Resquest video1= restTemplate.getForObject(url+url2,Resquest.class);
               String port= restTemplate.getUriTemplateHandler().expand(url,Video.class)+"";
               System.out.println(port);
//               TimeUnit.SECONDS.sleep(5);
                video.setSendInfo(video1.getData().toString());
//              restTemplate.getForObject(ord
//              erServices.get(0).getUri()+"/api/order/c/addVideoOrder?id="+video.getId()+"&title="+video.getTitle(), resgistor.Video.class,video);
//              return resquest.sueccess(200, "成功", orderServices.get(0));
//               return resquest.sueccess(200,"成功",video.getSendInfo());
//               return resquest.sueccess(200, "成功", video);
               return 5;
           }
        }
        return 6;
//        return resquest.error(401, "失败");
    }
}
